package com.yishe.config.db;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import tk.mybatis.spring.annotation.MapperScan;

import javax.sql.DataSource;


@Slf4j
@Configuration
@MapperScan(basePackages = "com.yishe.mapper.mysql", sqlSessionTemplateRef = "mysqlSqlSessionTemplate")
@EnableTransactionManagement
public class MySQLMybatisConfig extends MySQLDataSourceConfig {

    /** mysql 连接地址 **/
    @Value(value = "${spring.datasource.mysql.url}")
    private String url;

    /** mysql 用户名称 **/
    @Value(value = "${spring.datasource.mysql.username}")
    private String user_name;

    /** mysql 用户密码 **/
    @Value(value = "${spring.datasource.mysql.password}")
    private String password;

    @Override
    @Bean
    public DataSource mysqlDataSource() {
        if (StringUtils.isNotBlank(url)) {
            log.info("数据库参数覆盖 url 值为: {} ", url);
            this.setUrl(url);
        }
        if (StringUtils.isNotBlank(user_name)) {
            log.info("数据库参数覆盖 username 值为: {} ", user_name);
            this.setUserName(user_name);
        }
        if (StringUtils.isNotBlank(password)) {
            log.info("数据库参数覆盖 password .");
            this.setPassword(password);
        }
        return super.mysqlDataSource();
    }
}
